﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class GlobalInventoryRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F13611;
        protected GridView gvGlobalInventory;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblGlobalInventoryRptList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2GlobalInventoryList(GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity)
        {
            if ((globalInventoryRptQueryEntity.WhID != null) && (globalInventoryRptQueryEntity.WhID.Count > 0))
            {
                this.PagerFooter.CurrentPage = int.Parse(globalInventoryRptQueryEntity.CurrentPage);
                DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetGlobalInventoryRptDataSet(globalInventoryRptQueryEntity);
                base.BindData2GridView(this.gvGlobalInventory, ds);
                if ((ds != null) && (ds.Tables.Count > 1))
                {
                    this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                    this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
                }
            }
            else
            {
                this.lblMsg.Text = "请最少选择一个仓库查询！";
                this.lblMsg.Visible = true;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            globalInventoryRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = globalInventoryRptQueryEntity.CurrentPage;
            globalInventoryRptQueryEntity.CurrentPage = "0";
            this.gvGlobalInventory.Columns[1].Visible = false;
            this.gvGlobalInventory.AllowSorting = false;
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
            this.gvGlobalInventory.ShowFooter = false;
            globalInventoryRptQueryEntity.IsGetAll = false;
            globalInventoryRptQueryEntity.CurrentPage = currentPageIndex;
            this.SetGlobalInventoryQueryEntity(globalInventoryRptQueryEntity);
            base.Export2Excel("GlobalInventoryRpt.xls", this.gvGlobalInventory);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            globalInventoryRptQueryEntity.CurrentPage = "0";
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            globalInventoryRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            globalInventoryRptQueryEntity.SKU_ID = "";
            globalInventoryRptQueryEntity.OWNER_ID = "";
            globalInventoryRptQueryEntity.DESCR = "";
            globalInventoryRptQueryEntity.EXTERNAL_LOT = "";
            globalInventoryRptQueryEntity.SKU_CLASS_ID = "";
            globalInventoryRptQueryEntity.RECEIPT_DATE = "";
            globalInventoryRptQueryEntity.SHIPPED_DATE = "";
            globalInventoryRptQueryEntity.VENDOR_ID = "";
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            if (int.Parse(globalInventoryRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                globalInventoryRptQueryEntity.CurrentPage = (int.Parse(globalInventoryRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            if (int.Parse(globalInventoryRptQueryEntity.CurrentPage) > 0)
            {
                globalInventoryRptQueryEntity.CurrentPage = (int.Parse(globalInventoryRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
        }

        private GlobalInventoryRptQueryEntity GetGlobalInventoryQueryEntity()
        {
            return (base.PageQueryEntitySession as GlobalInventoryRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
                globalInventoryRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
            }
        }

        protected void gvGlobalInventory_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F13611", this.gvGlobalInventory);
        }

        protected void gvGlobalInventory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvGlobalInventory_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = this.GetGlobalInventoryQueryEntity();
                globalInventoryRptQueryEntity.SortField = e.SortExpression;
                if (globalInventoryRptQueryEntity.SortDirection == "ASC")
                {
                    globalInventoryRptQueryEntity.SortDirection = "DESC";
                }
                else if (globalInventoryRptQueryEntity.SortDirection == "DESC")
                {
                    globalInventoryRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity = new GlobalInventoryRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    globalInventoryRptQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    globalInventoryRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    globalInventoryRptQueryEntity.DESCR = this.PreviousPage.SkuDescr;
                    globalInventoryRptQueryEntity.EXTERNAL_LOT = this.PreviousPage.ExtenalLot;
                    globalInventoryRptQueryEntity.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    globalInventoryRptQueryEntity.VENDOR_ID = this.PreviousPage.VendorID;
                    if (!string.IsNullOrEmpty(this.PreviousPage.Wh))
                    {
                        globalInventoryRptQueryEntity.WhID = new List<string>();
                        string[] strWh = this.PreviousPage.Wh.Split(new char[] { ';' });
                        for (int i = 0; strWh.Length > i; i++)
                        {
                            globalInventoryRptQueryEntity.WhID.Add(strWh[i] + ".");
                        }
                    }
                }
                else
                {
                    globalInventoryRptQueryEntity.SKU_ID = "";
                    globalInventoryRptQueryEntity.OWNER_ID = "";
                    globalInventoryRptQueryEntity.DESCR = "";
                    globalInventoryRptQueryEntity.EXTERNAL_LOT = "";
                    globalInventoryRptQueryEntity.SKU_CLASS_ID = "";
                    globalInventoryRptQueryEntity.VENDOR_ID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            globalInventoryRptQueryEntity.IsGetAll = false;
            globalInventoryRptQueryEntity.CurrentPage = "0";
            globalInventoryRptQueryEntity.PageSize = this.gvGlobalInventory.PageSize.ToString();
            globalInventoryRptQueryEntity.SortField = "SKU_ID";
            globalInventoryRptQueryEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvGlobalInventory.PageSize;
            this.SetGlobalInventoryQueryEntity(globalInventoryRptQueryEntity);
            this.BindData2GlobalInventoryList(globalInventoryRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvGlobalInventory);
                this.LoadData();
            }
        }

        private void SetGlobalInventoryQueryEntity(GlobalInventoryRptQueryEntity globalInventoryRptQueryEntity)
        {
            base.PageQueryEntitySession = globalInventoryRptQueryEntity;
        }

        public GlobalInventoryRptQuery PreviousPage
        {
            get
            {
                return (GlobalInventoryRptQuery) base.PreviousPage;
            }
        }
    }
}

